package com.xywy.find;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;
import com.xywy.find.bean.JkzhType;

/**
 * 健康知乎
 * 
 * @author xiaogd
 * 
 */
@SuppressLint("Override")
public class JkzhActivity extends BaseActivity {
	private static final String TAG = "JkzhActivity";
	private TextView tvYsjk;
	private TextView tvXl;
	private TextView tvGxy;
	private TextView tvYjk;
	private TextView tvHyby;
	private TextView tvJf;
	private TextView tvTyb;
	private TextView tvYe;
	private TextView tvSex;

	private List<JkzhType> types;
	private int target = 0;
	private String title = "";
	private HashMap<String, Integer> map = new HashMap<String, Integer>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.find_jkzh_index);
		this.preInit();
		this.initView();
		this.loadData();
	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.find_jkzh_title_bar);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		titleBar.onClickBack(this);
		tvTitle.setText(getString(R.string.find_2));
		tvBack.setText(getString(R.string.main_tab_tv_find));
		//
		tvYsjk = (TextView) findViewById(R.id.tv_jkzh_ysjk);
		tvXl = (TextView) findViewById(R.id.tv_jkzh_xl);
		tvGxy = (TextView) findViewById(R.id.tv_jkzh_gxy);
		tvYjk = (TextView) findViewById(R.id.tv_jkzh_yjk);
		tvHyby = (TextView) findViewById(R.id.tv_jkzh_hyby);
		tvJf = (TextView) findViewById(R.id.tv_jkzh_jf);
		tvTyb = (TextView) findViewById(R.id.tv_jkzh_tyb);
		tvYe = (TextView) findViewById(R.id.tv_jkzh_ye);
		tvSex = (TextView) findViewById(R.id.tv_jkzh_sex);
	}

	/**
	 * 事件处理
	 * 
	 * 
	 * @param view
	 */
	public void onClickJkzh(View view) {
		int zhid = -1; // 下个页面用到
		switch (view.getId()) {
		case R.id.tv_jkzh_ysjk:

			tvYsjk.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_ysjk_pressed), null, null);
			target = 1;
			zhid = 1;
			title = getString(R.string.find_jkzh_ysjk);
			break;
		case R.id.tv_jkzh_xl:
			tvXl.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_xl_pressed), null, null);
			target = 3;
			zhid = 3;
			title = getString(R.string.find_jkzh_xl);
			break;
		case R.id.tv_jkzh_gxy:
			tvGxy.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_gxy_pressed), null, null);
			target = 7;
			zhid = 7;
			title = getString(R.string.find_jkzh_gxy);
			break;
		case R.id.tv_jkzh_yjk:
			tvYjk.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_yjk_pressed), null, null);
			target = 4;
			zhid = 4;
			title = getString(R.string.find_jkzh_yjk);
			break;
		case R.id.tv_jkzh_hyby:
			tvHyby.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_hyyb_pressed), null, null);
			target = 5;
			zhid = 5;
			title = getString(R.string.find_jkzh_hyby);
			break;
		case R.id.tv_jkzh_jf:
			tvJf.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_jf_pressed), null, null);
			target = 8;
			zhid = 8;
			title = getString(R.string.find_jkzh_jf);
			break;
		case R.id.tv_jkzh_tyb:
			tvTyb.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_tyb_pressed), null, null);
			target = 2;
			zhid = 2;
			title = getString(R.string.find_jkzh_tyb);
			break;
		case R.id.tv_jkzh_ye:
			tvYe.setCompoundDrawables(null,
					getDrawable(R.drawable.find_jkzh_yr_pressed), null, null);
			target = 9;
			zhid = 9;
			title = getString(R.string.find_jkzh_ye);
			break;
		case R.id.tv_jkzh_sex:
			target = 6;
			zhid = 6;
			title = getString(R.string.find_jkzh_sex);
			Log.e("良性", title);
			break;
		}
		;
		Stattistics(title);
		Intent intent = new Intent(JkzhActivity.this, JkzhPlazeActivity.class);
		intent.putExtra("target", target);
		intent.putExtra("title", title);
		intent.putExtra("id", zhid);
		startActivity(intent);
	}

	private Drawable getDrawable(int drawableInt) {
		Drawable drawable = getResources().getDrawable(drawableInt);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		return drawable;
	}

	@Override
	public void preInit() {

	}

	@Override
	public void loadData() {
		// this.sendMsg();
	}

	MyProgress myProgress;
	JSONObject back = null;
	/**
	 * 返回
	 */
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
			myProgress.stop();
			if (msg.obj == null)
				return;
			try {
				back = new JSONObject(msg.obj.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			types = getJkzhType(back.optJSONArray("data"));
		}
	};

	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求
	public void sendMsg() {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		paramsParser = new Parser();

		// 封装参数
		params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.find_jkzh_typelist);
		// 请求方式
		paramsParser.request = "get";

		// 请求参数
		// params.put("plaze",0 );

		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.obj = resultParser.back;
			handler.sendMessage(message);
		}
	};

	/**
	 * 封装返回的json
	 */
	private List<JkzhType> getJkzhType(JSONArray array) {
		List<JkzhType> list = Collections
				.synchronizedList(new ArrayList<JkzhType>());

		JkzhType type = new JkzhType();
		try {
			map.clear();
			if (null != array && array.length() > 0) {
				for (int i = 0; i < array.length(); i++) {
					JSONObject json = array.getJSONObject(i);

					JkzhType cltype = type.clone();
					map.put(json.optString("name"), json.optInt("id"));
					cltype.setId(json.optInt("id"));
					cltype.setName(json.optString("name"));
					list.add(cltype);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 友盟统计
	 */
	public void Stattistics(String title){
		Map<String, String> click = new HashMap<String, String>();
		click.put("plate",title);
		MobclickAgent.onEvent(this, "B00121", click);
	}
}
